﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using QuanLyTiemVang.DTO;

namespace QuanLyTiemVang.DAO
{
    class SanPhamDAO
    {
        private static QUANLYTIEMVANGEntities db = new QUANLYTIEMVANGEntities();

        static public List<SANPHAM> SelectSanPhamLikeMaSanPham(SANPHAM sp)
        {
            var query = (from d in db.SANPHAMs
                        where d.MaSanPham == sp.MaSanPham
                        select d).ToList();
            if (query.Count()>0)
                return query;
            return null;
        }
        static public List<SANPHAM> SelectSanPhamLikeTenSanPham(SANPHAM sp)
        {
            var query = from d in db.SANPHAMs
                        where d.TenSanPham.Contains(sp.TenSanPham)
                        select d;
            if (query.Count() >0)
                return query.ToList();
            return null;
        }
        static public List<SANPHAM> SelectSanPhamLikeLoaiSanPham(SANPHAM sp)
        {
            var query = from d in db.SANPHAMs
                        where sp.MaLoaiSanPham == d.MaLoaiSanPham
                        select d;
            if (query.Count()>0)
                return query.ToList();
            return null;
        }
        static public List<SANPHAM> SelectSanPhamBySoLuongTon(int soluongtonmin = -1, int soluongtonmax = -1)
        {
            var query = from d in db.SANPHAMs
                        select d;

            if (soluongtonmin != -1)
            {
                query = query.Where(m => m.SoLuongTon >= soluongtonmin);
            }

            if (soluongtonmax != -1)
            {
                query = query.Where(m => m.SoLuongTon <= soluongtonmax);
            }

            if (query.Count() > 0)
                return query.ToList();
            return null;
        }
        static public SANPHAM SelectSanPhamByMaSanPham(SANPHAM sp)
        {
            var query=db.SANPHAMs.Find(sp.MaSanPham);
            if (query != null)
                return query;
            return null;
        }
        static public List<SANPHAM> GetAllSanPham()
        {
            return db.SANPHAMs.ToList();
        }
        public static void UpdateSoLuongTon(SANPHAM sp)
        {
            var query = db.SANPHAMs.Find(sp.MaSanPham);
            if (query != null)
            {
                query.SoLuongTon = sp.SoLuongTon;
                db.SaveChanges();
            }
        }
        public static void Insert(SANPHAM sp)
        {
            db.SANPHAMs.Add(sp);
            db.SaveChanges();
        }
        public static void Delete(SANPHAM sp)
        {
            var query = db.SANPHAMs.Find(sp.MaSanPham);
            if (query != null)
            {
                db.SANPHAMs.Remove(query);
                db.SaveChanges();
            }
        }
        public static void Update(SANPHAM sp)
        {
            var query = db.SANPHAMs.Find(sp.MaSanPham);
            if (query != null)
            {
                query.TenSanPham = sp.TenSanPham;
                query.MaLoaiSanPham = sp.MaLoaiSanPham;
                db.SaveChanges();
            }
        }

        public static List<LOAISANPHAM> GetAllLoaiSanPham()
        {
            return db.LOAISANPHAMs.ToList();
        }

        //news Tam
        public static List<int> GetAllSanPhamId()
        {
            var query = from sp in db.SANPHAMs
                        select sp.MaSanPham;
            if (query == null)
                return null;
            return query.ToList();
        }

        public static string getTenSanPhambyMaSanPham(int masanpham)
        {
            var query = from sanpham in db.SANPHAMs
                        where sanpham.MaSanPham == masanpham
                        select sanpham;

            if (query.Count() > 0)
            {
                return query.First().TenSanPham.ToString();
            }
            return null;
        }


        // Thien
        public static List<String> GetAllTenLoaiSanPham()
        {
            List<LOAISANPHAM> lstLoaiSP = db.LOAISANPHAMs.ToList();
            List<String> lstName = new List<String>();
            foreach (var item in lstLoaiSP)
            {
                lstName.Add(item.TenLoaiSanPham);
            }
            return lstName;
        }    

        //Tam
        public static int getMaloaiSanPham(String tenloaisanpham)
        {
            int maloaisanpham = 0;
            List<LOAISANPHAM> lstLoaiSP = db.LOAISANPHAMs.ToList();
            foreach (LOAISANPHAM item in lstLoaiSP)
            {
                if (item.TenLoaiSanPham.ToString().Equals(tenloaisanpham))
                {
                    return item.MaLoaiSanPham;
                }
            }
            return maloaisanpham;
        }
        public static SANPHAM SelectSanPhambyTenSanPham(SANPHAM sp)
        {
            var query = from d in db.SANPHAMs
                        where d.TenSanPham == sp.TenSanPham
                        select d;
            if (query.Count() > 0)
            {
                return query.First();
            }
            return null;
        }
        public static int GetLastMaSanPham()
        {
            var query = (from d in db.SANPHAMs
                        select d.MaSanPham).ToList();

            if (query.Count == 0)
            {
                return 1;
            }

            return query.Max() + 1;
        }
    }
}
